import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AppService } from '../../services/admin/app.service';
import { H5Service } from '../../services/admin/h5.service';

/**
 * AppController
 * 对应 PHP: App Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('channel')
@Controller('adminapi/channel')
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly h5Service: H5Service,
  ) {}

  /**
   * 获取APP配置信息
   * 路由: GET app/config
   * PHP路由: Route::get('app/config', 'channel.App/get')
   */
  @Get('app/config')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async get(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: get

      return await this.appService.getConfig();
    } catch (error) {
      throw new BusinessException('get操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: PUT app/config
   * PHP路由: Route::put('app/config', 'channel.App/set')
   */
  @Put('app/config')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async set(@Body() data: SetDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: set

      return await this.appService.setConfig(data);
    } catch (error) {
      throw new BusinessException('set操作失败', error);
    }
  }

  /**
   * versionList
   * 路由: GET app/version
   * PHP路由: Route::get('app/version', 'channel.App/versionList')
   */
  @Get('app/version')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'versionList' })
  async versionList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: versionList

      return await this.appService.getVersionPage(data);
    } catch (error) {
      throw new BusinessException('versionList操作失败', error);
    }
  }

  /**
   * versionInfo
   * 路由: GET app/version/:id
   * PHP路由: Route::get('app/version/:id', 'channel.App/versionInfo')
   */
  @Get('app/version/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'versionInfo' })
  async versionInfo(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: versionInfo

      return await this.appService.getVersionInfo(id);
    } catch (error) {
      throw new BusinessException('versionInfo操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: POST app/version
   * PHP路由: Route::post('app/version', 'channel.App/add')
   */
  @Post('app/version')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.appService.addVersion(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: PUT app/version/:id
   * PHP路由: Route::put('app/version/:id', 'channel.App/edit')
   */
  @Put('app/version/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.appService.editVersion(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: DELETE app/version/:id
   * PHP路由: Route::delete('app/version/:id', 'channel.App/del')
   */
  @Delete('app/version/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async del(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.appService.delVersion(id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * appPlatform
   * 路由: GET app/platfrom
   * PHP路由: Route::get('app/platfrom', 'channel.App/appPlatform')
   */
  @Get('app/platfrom')
  @ApiOperation({ summary: 'appPlatform' })
  async appPlatform() {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: appPlatform
      // 直接返回字典数据
      return AppDict.getAppPlatform();
    } catch (error) {
      throw new BusinessException('appPlatform操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: GET app/build/log/:key
   * PHP路由: Route::get('app/build/log/:key', 'channel.App/buildLog')
   */
  @Get('app/build/log/:key')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async buildLog(@Param('key') key: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: buildLog

      return await this.appService.getBuildLog(key);
    } catch (error) {
      throw new BusinessException('buildLog操作失败', error);
    }
  }

  /**
   * 获取APP配置信息
   * 路由: PUT app/version/:id/release
   * PHP路由: Route::put('app/version/:id/release', 'channel.App/release')
   */
  @Put('app/version/:id/release')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '获取APP配置信息' })
  async release(
    @Param('id') id: string,
    @Body() data: ReleaseDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: release

      return await this.appService.release(id);
    } catch (error) {
      throw new BusinessException('release操作失败', error);
    }
  }

  /**
   * generateSingCert
   * 路由: POST app/generate_sing_cert
   * PHP路由: Route::post('app/generate_sing_cert', 'channel.App/generateSingCert')
   */
  @Post('app/generate_sing_cert')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'generateSingCert' })
  async generateSingCert(
    @Body() data: GenerateSingCertDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: generateSingCert

      return await this.appService.generateSingCert(data);
    } catch (error) {
      throw new BusinessException('generateSingCert操作失败', error);
    }
  }
}
